using Fine.Collections.Const;
using Fine.TestUtils;
using NUnit.Framework;



namespace Fine.Collections.Cont
{
    [TestFixture]
    public class ArrList_test
    {

        [Test]
        public void Test_Basic0()
        {
            var list = new ArrList<long>(1);

            list.Count       ._equals_(0);
            list.IsEmpty     ._true_();
            list.IsNotEmpty  ._false_();
        }


        [Test]
        public void Test_Basic1()
        {
            var list = new ArrList<long>(1);

            list.Add(777L);

            list.Count       ._equals_(1);
            list.IsEmpty     ._false_();
            list.IsNotEmpty  ._true_();
        }


        [Test]
        public void Test_Basic3()
        {
            var list = new ArrList<long>(1, 3);

            list.Add(1111L);
            list.Add(2222L);
            list.Add(3333L);

            list.Count  ._equals_(3);
            list[1]     ._equals_(1111L);
            list[2]     ._equals_(2222L);
            list[3]     ._equals_(3333L);
        }


        [Test]
        public void Test_Floats()
        {
            var list = new ArrList<float>(1, 3);

            list.Add(1.0f);
            list.Add(2.718281828f);
            list.Add(3.14159265f);

            list[1]     ._equals_(1.0f);
            list[2]     ._equals_(2.718281828f);
            list[3]     ._equals_(3.14159265f);
        }


        [Test]
        public void test_IteratorToTheEnd()
        {
            ArrList<long> list = new ArrList<long>(1,10) {3333, 4444};
            var it = list.TakeIterator();

            it.Next();
            Assert.That(it.Current, Is.EqualTo(3333));
            it.Next();
            Assert.That(it.Current, Is.EqualTo(4444));
            it.Next();
        }


        [Test]
        public void test_AddAllFromArray()
        {
            var list = new ArrList<ulong>(1, 3);
            list.Add(1111uL);
            list.Add(2222uL);

            var array = new ulong[] {3333uL, 4444uL};

            list.AddAll(array);

            Assert.That(list[1], Is.EqualTo(1111uL));
            Assert.That(list[2], Is.EqualTo(2222uL));
            Assert.That(list[3], Is.EqualTo(3333uL));
            Assert.That(list[4], Is.EqualTo(4444uL));

            Assert.That(list.Count, Is.EqualTo(4));
        }


        [Test]
        public void test_AddAllFromArray_Slice()
        {
            var list = new ArrList<long>(1, 3);
            list.Add(1111);
            list.Add(2222);

            var array = new long[] {-0, -1, -2, 3333, 4444, -5, -6, -7};

            list.AddAll(array, 3, 2);

            Assert.That(list[1], Is.EqualTo(1111L));
            Assert.That(list[2], Is.EqualTo(2222L));
            Assert.That(list[3], Is.EqualTo(3333L));
            Assert.That(list[4], Is.EqualTo(4444L));

            Assert.That(list.Count, Is.EqualTo(4));
        }


        [Test]
        public void test_AddAllFromArrayList()
        {
            var list = new ArrList<ulong>(1, 6);
            list.Add(1111);
            list.Add(2222);
            list.Add(3333);
            list.Add(4444);

            var src = new ArrList<ulong>(1, 7);
            src.Add(5555);
            src.Add(6666);
            src.Add(7777);
            src.Add(8888);

            list.AddAll(src);
            
            Assert.That(list.Count, Is.EqualTo(8));
            Assert.That(list[1], Is.EqualTo(1111));
            Assert.That(list[2], Is.EqualTo(2222));
            Assert.That(list[3], Is.EqualTo(3333));
            Assert.That(list[4], Is.EqualTo(4444));
            Assert.That(list[5], Is.EqualTo(5555));
            Assert.That(list[6], Is.EqualTo(6666));
            Assert.That(list[7], Is.EqualTo(7777));
            Assert.That(list[8], Is.EqualTo(8888));
        }


        [Test]
        public void test_AddAllFromConstList()
        {
            var list = new ArrList<ulong>(1, 6);
            list.Add(1111);
            list.Add(2222);
            list.Add(3333);
            list.Add(4444);

            var src = CC1.ListOf(5555uL, 6666uL, 7777uL, 8888uL);

            list.AddAll(src);
            
            Assert.That(list.Count, Is.EqualTo(8));
            Assert.That(list[1], Is.EqualTo(1111));
            Assert.That(list[2], Is.EqualTo(2222));
            Assert.That(list[3], Is.EqualTo(3333));
            Assert.That(list[4], Is.EqualTo(4444));
            Assert.That(list[5], Is.EqualTo(5555));
            Assert.That(list[6], Is.EqualTo(6666));
            Assert.That(list[7], Is.EqualTo(7777));
            Assert.That(list[8], Is.EqualTo(8888));
        }


        [Test]
        public void test_AddAllFromCollection()
        {
            var list = new ArrList<ulong>(1, 6);
            list.Add(1111);
            list.Add(2222);
            list.Add(3333);
            list.Add(4444);

            var src = new LinkList<ulong>();
            src.Add(5555);
            src.Add(6666);
            src.Add(7777);
            src.Add(8888);

            list.AddAll(src);

            Assert.That(list.Count, Is.EqualTo(8));
            Assert.That(list[1], Is.EqualTo(1111));
            Assert.That(list[2], Is.EqualTo(2222));
            Assert.That(list[3], Is.EqualTo(3333));
            Assert.That(list[4], Is.EqualTo(4444));
            Assert.That(list[5], Is.EqualTo(5555));
            Assert.That(list[6], Is.EqualTo(6666));
            Assert.That(list[7], Is.EqualTo(7777));
            Assert.That(list[8], Is.EqualTo(8888));
        }


        /*
        [Test]
        public void test_AddAllFromMicrosoftList()
        {
            var list = new ArrList<ulong>(1, 6);
            list.Add(1111);
            list.Add(2222);
            list.Add(3333);
            list.Add(4444);

            var src = new System.Collections.Generic.List<long>();
            src.Add(5555);
            src.Add(6666);
            src.Add(7777);
            src.Add(8888);

            list.AddAll(src);
            
            Assert.That(list.Count, Is.EqualTo(8));
            Assert.That(list[1], Is.EqualTo(1111));
            Assert.That(list[2], Is.EqualTo(2222));
            Assert.That(list[3], Is.EqualTo(3333));
            Assert.That(list[4], Is.EqualTo(4444));
            Assert.That(list[5], Is.EqualTo(5555));
            Assert.That(list[6], Is.EqualTo(6666));
            Assert.That(list[7], Is.EqualTo(7777));
            Assert.That(list[8], Is.EqualTo(8888));
        }
        */


        [Test]
        public void test_TurnIntoConst_emptyJustCreated()
        {
            ArrList<long> arrlist = new ArrList<long>(1);
            ConstList<long> clist = arrlist.TurnIntoConst();

            clist          ._is_not_null_();
            clist.Count    ._equals_(0);
            clist.IsEmpty  ._true_();
        }


        [Test]
        public void test_TurnIntoConst_emptyUsed()
        {
            ArrList<long> arrlist = new ArrList<long>(1,3);
            arrlist.Add(11111111);
            arrlist.Add(22222222);
            arrlist.Add(33333333);
            arrlist.Add(44444444);
            arrlist.Add(55555555);
            arrlist.Add(66666666);
            arrlist.Add(77777777);
            arrlist.Add(88888888);

            for (int i = 1; i <= 8; i++)
                arrlist.ExcludeFirst();

            ConstList<long> clist = arrlist.TurnIntoConst();

            clist          ._is_not_null_();
            clist.Count    ._equals_(0);
            clist.IsEmpty  ._true_();
        }


        [Test]
        public void test_TurnIntoConst_basic()
        {
            ArrList<long> arrlist = new ArrList<long>(1, 3);
            arrlist.Add(11111111);
            arrlist.Add(22222222);
            arrlist.Add(33333333);
            arrlist.Add(44444444);

            ConstList<long> clist = arrlist.TurnIntoConst();

            clist[1] ._equals_(11111111L);
            clist[2] ._equals_(22222222L);
            clist[3] ._equals_(33333333L);
            clist[4] ._equals_(44444444L);

            arrlist.Add(88888888L);

            clist.Contains(88888888L)   ._false_();
        }


        [Test]
        public void test_TurnIntoConst_arrayIsTheSame()
        {
            ArrList<long> arrlist = new ArrList<long>(1, 3);
            arrlist.Add(11111111);
            arrlist.Add(22222222);
            arrlist.Add(33333333);
            arrlist.Add(44444444);

            var wasAr = arrlist.ar;

            ConstList<long> clist = arrlist.TurnIntoConst();

            clist.arr   ._same_(wasAr);
        }


        [Test]
        public void test_TurnIntoConst_detached()
        {
            ArrList<long> arrlist = new ArrList<long>(1, 3);
            arrlist.Add(11111111);
            arrlist.Add(22222222);

            var wasAr = arrlist.ar;

            arrlist.TurnIntoConst();

            arrlist.ar         ._not_same_(wasAr);
            arrlist.count      ._equals_(0);
            arrlist.lastIndex  ._less_(arrlist.firstIndex);
        }


        [Test]
        public void test_TurnIntoConst_bounds()
        {
            ArrList<ushort> arrlist = new ArrList<ushort>(1001);
            arrlist.Add(44);
            arrlist.Add(55);
            arrlist.Add(66);

            var clist = arrlist.TurnIntoConst();

            clist.firstIndex  ._equals_(1001);
            clist.lastIndex   ._equals_(1003);
        }
    }
}